#!/bin/sh

tz='MSK-4MSK'
export TZ=$tz

[ -f /tmp/hdd/volumes/HDD1/nosched.lock ] && exit 0

CRON="/opt/etc/crontab.tor"
CFG="/opt/etc/trans.cfg"

if [ ! -s $CRON ]; then
  echo -n $D >> /opt/var/log/transmission-daemon.log
  echo "File [$CRON] not found" >> /opt/var/log/transmission-daemon.log
  exit 1
fi
if [ ! -s $CFG ]; then
  echo -n $D >> /opt/var/log/transmission-daemon.log
  echo "File [$CFG] not found" >> /opt/var/log/transmission-daemon.log
  exit 2
fi
. $CFG

tz='MSK-4MSK'
export TZ=$tz

C=`cat $CRON | grep -v "#"`
W=`date +%u`
H=`date +%k`
H=$((H+1))
for i in $C
do
    WDAY=`echo "$i" | cut -d";" -f1`
    TIMES=`echo "$i" | cut -d";" -f2`
    if [ $WDAY -eq $W ]; then
        T=`echo $TIMES | cut -d"," -f$H`
        case $T in
            0) COMMAND="-t all -S";;
            1) COMMAND="-t all -as";;
            2) COMMAND="-t all -AS";;
            *) COMMAND="";;
        esac
    fi
done

date > /opt/var/log/transmission-daemon.log
/usr/local/etc/mos/trans/transmission-remote $thost:$tport -n "$tuser:$tpass" $COMMAND >>/opt/var/log/transmission-daemon.log 2>>/opt/var/log/transmission-daemon.log